﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Linq;
using System.Text;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{

    public class YBWGGMOVData : BaseFunction
    {
        public bool flagRegenerateLastMonthData;
        ArrayList ListTris = new ArrayList();

        List<WGSummary> ListWGSummary;
        string TrisInclude = "";

        public int ThreadCount;
        private string OrderNum;
        DataTable tbWGSummary;
        public YBWGGMOVData()
            : base()
        {
            BuildTrisList();
            tbWGSummary = new DataTable("WGGSUM");
            tbWGSummary.Columns.Add("ARTICLE", typeof(string));
            tbWGSummary.Columns.Add("WBBDPART", typeof(string));
            tbWGSummary.Columns.Add("FACTORY", typeof(string));
            tbWGSummary.Columns.Add("WBEWMTR", typeof(double));
            tbWGSummary.Columns.Add("PCNUM", typeof(int));
        }
        public YBWGGMOVData(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
        }
        private void BuildTrisList()
        {
            ListTris = new ArrayList();
            TrisType tristype = null;
            TrisInclude = "01;10;11;13;21;27;32;";
            tristype = new TrisType("01", "+", "净长"); ListTris.Add(tristype);
            tristype = new TrisType("10", "+", "货物收入"); ListTris.Add(tristype);
            tristype = new TrisType("11", "+", "货物收入"); ListTris.Add(tristype);
            tristype = new TrisType("12", "-", "销售返回"); ListTris.Add(tristype);
            tristype = new TrisType("13", "+", "产品录入"); ListTris.Add(tristype);
            tristype = new TrisType("20", "-", "货物发出"); ListTris.Add(tristype);
            tristype = new TrisType("21", "+", "货物进入"); ListTris.Add(tristype);
            tristype = new TrisType("26", "-", "旧布卷减少"); ListTris.Add(tristype);
            tristype = new TrisType("27", "+", "新布卷增加"); ListTris.Add(tristype);
            tristype = new TrisType("31", "-", "发货"); ListTris.Add(tristype);
            tristype = new TrisType("32", "+", "退货"); ListTris.Add(tristype);
            tristype = new TrisType("36", "-", "生产消耗"); ListTris.Add(tristype);
            tristype = new TrisType("40", "-", "等级修改减少"); ListTris.Add(tristype);
            tristype = new TrisType("41", "-", "等级修改增加"); ListTris.Add(tristype);
            tristype = new TrisType("42", "+", "库存修正减少"); ListTris.Add(tristype);
            tristype = new TrisType("43", "+", "库存修正增加"); ListTris.Add(tristype);
            tristype = new TrisType("48", "-", "生产差异-"); ListTris.Add(tristype);
            tristype = new TrisType("49", "+", "生产差异+"); ListTris.Add(tristype);
            tristype = new TrisType("51", "+", "消耗差异"); ListTris.Add(tristype);
            tristype = new TrisType("61", "-", "留位"); ListTris.Add(tristype);
            tristype = new TrisType("62", "+", "取消留位"); ListTris.Add(tristype);
            tristype = new TrisType("64", "+", "留位剪布"); ListTris.Add(tristype);
            tristype = new TrisType("65", "+", "锁定"); ListTris.Add(tristype);
            tristype = new TrisType("66", "+", "解锁"); ListTris.Add(tristype);
            tristype = new TrisType("69", "+", "其他修正"); ListTris.Add(tristype);
            tristype = new TrisType("80", "+", "发票数量"); ListTris.Add(tristype);
            tristype = new TrisType("88", "-", "价值更改-"); ListTris.Add(tristype);
            tristype = new TrisType("89", "+", "价值更改"); ListTris.Add(tristype);
            tristype = new TrisType("IN", "+", "系统自动调节"); ListTris.Add(tristype);

        }
        public DataSet GetRawData(DataRow dwABPOS)
        {
            DataSet set4Return = new DataSet();
            DataSet setLocal = new DataSet();
            DataSet setResult = new DataSet();
            DataSet setTemp = new DataSet();
            DataTable tableLocal = null;
            string VKPAUFNUM;
            string WBSRTDATE, WBENTDATE;
            WBSRTDATE = dwABPOS["WBSRTDATE"].ToString().Trim();
            WBENTDATE = dwABPOS["WBENTDATE"].ToString().Trim();
            try
            {
                OracleCommand or1 = OracleDB.CreateCommand();
                #region
                /*
                 SELECT  WBBEINDAT AS WBEWBUCHDAT,"
                    + " TRIM(WBEWART)||'-'||TRIM(WBEWFARBE)||'-'||TRIM(WBEWVAR) AS ARTICLE,WBEWBS,WBBDPART, "
                    + "WBEWFC,SUM(CASE WHEN WBEWMTR <0 THEN -1 ELSE 1 END ) AS PCNUM,ROUND(SUM(WBEWMTR)*0.001,2) AS WBEWMTR"
                    + " FROM WBBEW W JOIN WBBST B ON WBEWSTKNR=WBBSTKNR"
                    + " WHERE WBEWWAKZ='R' AND WBBEINDAT>='" + WBSRTDATE + "' AND WBBEINDAT<='" + WBENTDATE + "'"
                    + " AND (WBEWBS IN ('01','10','11') OR (WBEWBS = '13' AND WBEWFC = 'WB15')) "
                    + " GROUP BY WBEWART,WBEWFARBE,WBEWVAR,WBEWBS,WBBDPART,WBEWFC,WBBEINDAT"
                    + " ORDER BY 1,2,3
                 */
                or1.CommandText = "SELECT /* +FIRST_ROWS*/ "
                    + " (CASE WHEN WBEWMTR < 0 THEN WBBEINDAT ELSE WBEWWERTDAT END ) AS WBEWBUCHDAT,WBEWBUCHDAT as DAT, "
                    + "        DECODE(ABFAUFKRS, 'WS', 'SAMPLE', 'BULK') AS TYPE, "
                    + "        TRIM(WBEWART) || '-' || TRIM(WBEWFARBE) || '-' || TRIM(WBEWVAR) AS ARTICLE, "
                    + "        WBEWBS,WBBDPART,TRIM(D.LSNAME1) AS FACTORY,WBEWFC, "
                    + "        (CASE  WHEN WBEWMTR < 0 THEN  -1 ELSE  1  END) AS PCNUM, "
                    + "        ROUND((WBEWMTR) * 0.001, 2) AS WBEWMTR,E.WTGFDISM*0.01 AS FDISM, "
                    + "  B.WBBWBMNRKRS||'-'||TO_NUMBER(B.WBBWBMLFDNR)||'-'||TO_NUMBER(B.WBBWBMPOS) AS MACHINE "
                    + "   FROM WBBEW W, WBBST B, ABFOLGE C, LIEFDAT D,WTGEW E "
                    + "  WHERE W.WBEWMANDANT = B.WBBMANDANT "
                    + "    AND W.WBEWRWKRS = B.WBBRWKRS    AND W.WBEWRWLFD = B.WBBRWLFD "
                    + "    AND W.WBEWPOS = B.WBBPOS   AND W.WBEWMANDANT = C.ABFMANDANT "
                    + "    AND W.WBEWAUFKRS = C.ABFAUFKRS   AND W.WBEWAUFLFD = C.ABFAUFLFD "
                    + "    AND W.WBEWAUFPOS = C.ABFAUFPOS   AND W.WBEWAFOLGE = C.ABFAFOLGE "
                    + "    AND C.ABFMANDANT = D.LSMANDANT   AND C.ABFLIEFNR = D.LSLIEFNR "
                    + "    AND W.WBEWMANDANT = E.WTGMANDANT   AND W.WBEWKUNDNR = E.WTGKUNDNR "
                    + "    AND W.WBEWART = E.WTGART   AND W.WBEWVAR = E.WTGVAR "
                    + "    AND WBEWWAKZ = 'R'   AND WBEWBS = '13'   AND WBEWFC = 'WB15    ' "
                    + "    AND W.WBEWMANDANT = '00'   AND W.WBEWLO = 'YB' "                    
                    + " AND WBEWBUCHDAT>='" + WBSRTDATE + "' AND WBEWBUCHDAT<='" + WBENTDATE + "' "
                    + " UNION ALL "
                    + " SELECT /* +FIRST_ROWS*/ "
                    + " (CASE WHEN WBEWMTR < 0 THEN WBBEINDAT ELSE WBEWWERTDAT END ) AS WBEWBUCHDAT,WBEWBUCHDAT as DAT, "
                    + "       'UNKNOWN' AS TYPE, "
                    + "        TRIM(WBEWART) || '-' || TRIM(WBEWFARBE) || '-' || TRIM(WBEWVAR) AS ARTICLE, "
                    + "        WBEWBS, WBBDPART, 'YINGBANG' AS FACTORY, WBEWFC, "
                    + " (CASE WHEN WBEWMTR < 0 THEN -1 ELSE 1 END) AS PCNUM, "
                    + " ROUND((WBEWMTR) * 0.001, 2) AS WBEWMTR,E.WTGFDISM*0.01 AS FDISM, "
                    + "  B.WBBWBMNRKRS||'-'||TO_NUMBER(B.WBBWBMLFDNR)||'-'||TO_NUMBER(B.WBBWBMPOS) AS MACHINE "
                    + " FROM WBBEW W, WBBST B,WTGEW E "
                    + "  WHERE W.WBEWMANDANT = B.WBBMANDANT   AND W.WBEWRWKRS = B.WBBRWKRS "
                    + "    AND W.WBEWRWLFD = B.WBBRWLFD   AND W.WBEWPOS = B.WBBPOS "
                    + "    AND W.WBEWMANDANT = E.WTGMANDANT   AND W.WBEWKUNDNR = E.WTGKUNDNR "
                    + "    AND W.WBEWART = E.WTGART   AND W.WBEWVAR = E.WTGVAR "
                    + "    AND W.WBEWMANDANT = '00'   AND w.WBEWWAKZ = 'R' "
                    + "    AND w.WBEWBS IN ('01', '10')   AND W.WBEWLO = 'YB' "
                    + " AND WBEWBUCHDAT>='" + WBSRTDATE + "' AND WBEWBUCHDAT<='" + WBENTDATE + "' "
                    ; 
                #endregion
                setLocal = Connect2Proxy(or1);
                if (setLocal.Tables.Count > 0)
                {
                    tableLocal = setLocal.Tables[0];
                    tableLocal.TableName = "WBBEW";
                    SetDefault(tableLocal);
                    byte[] b;
                    string value; 
                    string sTemp = ""; 
                    string Column = ""; 
                    string ARTICLE = "",WBBDPART,FACTORY; 
                    int PCNUM;
                    double WBEWMTR;
                    List<DataRow> dltRows = new List<DataRow>();
                    ListWGSummary=new List<WGSummary>();
                    foreach (DataRow dwTemp in tableLocal.Rows)
                    {
                        Column = "ARTICLE";
                        #region
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[Column] = value;
                        #endregion
                        Column = "WBBDPART";
                        #region
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[Column] = value;
                        #endregion 
                        Column = "FACTORY";
                        #region
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[Column] = value;
                        #endregion 
                         if(FilterData(dwTemp, ref dltRows))
                         {
                               ARTICLE = dwTemp["ARTICLE"].ToString().Trim();
                             WBBDPART = dwTemp["WBBDPART"].ToString().Trim();
                             FACTORY = dwTemp["FACTORY"].ToString().Trim();
                             PCNUM =System.Convert.ToInt16(dwTemp["PCNUM"].ToString());
                             WBEWMTR = System.Convert.ToDouble(dwTemp["WBEWMTR"].ToString());
                             var query = from WGSummary wgsum in ListWGSummary
                                         where wgsum.ARTICLE == ARTICLE
                                         && wgsum.WBBDPART == WBBDPART
                                         && wgsum.FACTORY == FACTORY
                                         select wgsum;
                             WGSummary wgSum = null;
                             foreach (WGSummary wgsum in query)
                             {
                                 wgSum = wgsum;
                                 wgSum.PCNUM += PCNUM;                                 
                                 wgsum.WBEWMTR += WBEWMTR;
                             }
                             if (wgSum == null)
                             {
                                 wgSum = new WGSummary(FACTORY, ARTICLE, WBBDPART, PCNUM, WBEWMTR);
                                 ListWGSummary.Add(wgSum);
                             }
                         } 
                    }
                    foreach (DataRow dwTemp in dltRows)
                    {
                        dwTemp.Delete();
                    }
                    setLocal.AcceptChanges();
                    DataRow dwWGGSUM = null;
                    tbWGSummary.Clear();
                    foreach (WGSummary wgsum in ListWGSummary)
                    {
                        dwWGGSUM = tbWGSummary.NewRow();
                        dwWGGSUM["ARTICLE"] = wgsum.ARTICLE;
                        dwWGGSUM["WBBDPART"] = wgsum.WBBDPART;
                        dwWGGSUM["FACTORY"] = wgsum.FACTORY;
                        dwWGGSUM["PCNUM"] = wgsum.PCNUM;
                        dwWGGSUM["WBEWMTR"] = wgsum.WBEWMTR;
                        tbWGSummary.Rows.Add(dwWGGSUM);
                    }
                    set4Return.Merge(tbWGSummary);
                    set4Return.Merge(setLocal);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return set4Return;
        }
        private bool FilterData(DataRow dwWBBEW, ref List<DataRow> dltRows)
        {
            try
            {
                string WBEWBS = dwWBBEW["WBEWBS"].ToString().Trim();
                string WBEWFC = dwWBBEW["WBEWFC"].ToString().Trim();

                if (!TrisInclude.Contains(WBEWBS + ";"))
                {
                    if (WBEWBS == "21"
                        && WBEWFC == "WB16")
                    {
                        return true;
                    }
                    dltRows.Add(dwWBBEW);
                    return false;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return true;
        }
        public DataSet BuildWBBST(DataRow dwUI)
        {
            string progress = "查询完成";
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                EventDispatcher.Instance.RaiseStatus("查询开始", StatusEventType.StartProgress);
                setResult = GetRawData(dwUI);
                setReturn.Merge(setResult);
                EventDispatcher.Instance.RaiseStatus("查询完成", StatusEventType.StopProgress);
            }
            catch (Exception ex)
            {
                Tracer.Error(ABPOS + ex.StackTrace, ex);
            }
            return setReturn;
        }
    }
}